<?php
$title = '管理员总览';
/** @var array $stats */
/** @var array $signals */
/** @var array $orders */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
?>
<section class="section">
    <div class="app-container" style="display:flex;flex-direction:column;gap:24px;">
        <div class="grid grid-3">
            <div class="card">
                <span class="badge">24h 信号</span>
                <h2 style="margin:8px 0 0;"><?= htmlspecialchars((string)($stats['signals24h'] ?? 0)) ?></h2>
                <p style="color:var(--text-secondary);">TradingView 接入量（幂等后）</p>
            </div>
            <div class="card">
                <span class="badge">成功率</span>
                <h2 style="margin:8px 0 0;"><?= htmlspecialchars((string)($stats['successRate'] ?? '0%')) ?></h2>
                <p style="color:var(--text-secondary);">近 100 笔 ExecOrder 成功占比</p>
            </div>
            <div class="card">
                <span class="badge">错误 Top</span>
                <ul style="margin:8px 0 0;padding-left:18px;color:var(--text-secondary);">
                    <?php foreach ($stats['topErrors'] as $err => $count): ?>
                        <li><?= htmlspecialchars($err) ?> · <?= $count ?></li>
                    <?php endforeach; ?>
                    <?php if (empty($stats['topErrors'])): ?>
                        <li>暂无错误</li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
        <div class="card" style="gap:16px;">
            <div style="display:flex;justify-content:space-between;align-items:center;">
                <h3 style="margin:0;">最近信号</h3>
                <a class="btn-ghost btn" href="/admin/audit">查看审计</a>
            </div>
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>脚本</th>
                    <th>状态</th>
                    <th>时间</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($signals as $signal): ?>
                    <tr>
                        <td><?= htmlspecialchars((string)$signal['id']) ?></td>
                        <td><?= htmlspecialchars((string)$signal['script_id']) ?></td>
                        <td><?= htmlspecialchars((string)$signal['status']) ?></td>
                        <td><?= \Frontend\Helpers\Format::datetime($signal['created_at'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="card" style="gap:16px;">
            <h3 style="margin:0;">最近订单</h3>
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户</th>
                    <th>信号</th>
                    <th>状态</th>
                    <th>错误</th>
                    <th>时间</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($orders as $order): ?>
                    <tr>
                        <td><?= htmlspecialchars((string)$order['id']) ?></td>
                        <td><?= htmlspecialchars((string)$order['user_id']) ?></td>
                        <td><?= htmlspecialchars((string)($order['signal_id'] ?? '-')) ?></td>
                        <td><?= htmlspecialchars((string)$order['status']) ?></td>
                        <td><?= htmlspecialchars((string)($order['error'] ?? '-')) ?></td>
                        <td><?= \Frontend\Helpers\Format::datetime($order['created_at'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
<?php require __DIR__ . '/../partials/footer.php'; ?>
